package com.app.shop.activitys;


import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.shop.R;
import com.app.shop.adapters.GoodsListAdapter;
import com.app.shop.beans.GoodsBean;

import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.DialogUtil;
import com.app.library.utils.StringUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.widgts.CustomDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class  SearchActivity extends BaseActivity implements View.OnClickListener{


   private TextView tvTitle;

    private EditText edtSearch;

    private Button btSearch;

    private RecyclerView recy;
  private      ImageView imgvreturn;
    private List<GoodsBean> list = new ArrayList<>();
    private GoodsListAdapter adapter;

    private String content;


    private String dialog_title = "删除该商品";
    private String dialog_content = "是否确定操作？";
    private CustomDialog customDialog;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        edtSearch= (EditText) findViewById(R.id.edt_search);
        btSearch= (Button) findViewById(R.id.bt_search);
        recy= (RecyclerView) findViewById(R.id.recy);

            imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        btSearch.setOnClickListener(this::onClick);
        imgvreturn.setOnClickListener(this::onClick);

        tvTitle.setText("搜索");
        initAdapter();
    }

    private void initAdapter() {

        adapter = new GoodsListAdapter(R.layout.item_goods_list, list);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                bundle.putInt("from", 0);
                showActivity(SearchActivity.this, GoodsDetailActivity.class, bundle);
            }
        });


        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                del(position);
            }
        });
    }


    private void del(int position) {

        customDialog = DialogUtil.showDialog(this, customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                DataSupport.deleteAll(GoodsBean.class, "goods_id=?", list.get(position).getGoods_id());
                list.remove(position);
                adapter.notifyDataSetChanged();
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
                ToastUtil.showToast(SearchActivity.this, "删除成功");
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });

    }


    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            onSearch(content);
        }
    }


    private void onSearch(String content) {
        list.clear();
        List<GoodsBean> list_all = DataSupport.findAll(GoodsBean.class);
        for (int i = 0; i < list_all.size(); i++) {
            if (list_all.get(i).getGoods_name().contains(content)) {
                list.add(list_all.get(i));
            }
        }

        if (list.size() == 0) {
            ToastUtil.showToast(this, "没有结果");
        }

        adapter.notifyDataSetChanged();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.bt_search:
                if (StringUtil.isEmpty(edtSearch.getText().toString())) {
                    ToastUtil.showToast(this, "请输入搜索内容");
                    return;
                }
                content = edtSearch.getText().toString();
                onSearch(content);
                break;
        }
    }




}
